<?php
/**
 * 个人资料相关api
 * 此文件下所有操作均需要验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;

use Common\Common\Tools;
use Common\Controller\ApiController;

class UserController extends ApiController
{
    public function rest()
    {
        switch ($this->_method) {
            case 'get': // get请求处理代码
                $this->_get();
                break;
            case 'put': // get请求处理代码
                $this->_put();
                break;
        }
    }

    /**
     * 查看用户个人资料
     */
    private function _get()
    {
        $userid = $this->userId;
        $data['member_profile'] = D('MemberProfile')->get_member_profile($userid, 'userid,avatar,truename,sex,qq,areaid,address,instro', false);
        if (!$data['member_profile']) {
            $this->myApiPrint(C('STATUS')['211'], 400);
        }
        $allArea = D('Area')->get_all_area();
        $request = Tools::get_list_parents($allArea, I('get.areaid'), 'areaid', 'parentid');
        $data['parent_areas'] = $request;
        if(!$data){
            $this->myApiPrint(C('STATUS')['230'], 201, []);
        }else{
            $this->myApiPrint(C('STATUS')['200'], 200, $data);
        }
    }

    /**
     * 编辑个人资料
     */
    private function _put()
    {
        $map['userid'] = $this->userId; //获取到用户的ID
        $avatar = I('put.avatar');
        $true_name = I('put.truename');
        $sex = I('put.sex');
        $qq = I('put.qq');
        $areaid = I('put.areaid');
        $address = I('put.address');
        $instro = I('put.instro');
        $data['avatar'] = $avatar;
        $data['truename'] = $true_name;
        $data['sex'] = isset($sex) ? $sex : '0';
        $data['qq'] = !empty($qq) ? trim($qq) : '';
        $data['areaid'] = $areaid;
        $data['address'] = !empty($address) ? trim($address) : '';
        $data['instro'] = !empty($instro) ? trim($instro) : '';
        $info = D('MemberProfile')->where($map)->save($data);
        if (false !== $info) {
            $uid = $this->userId;
            $request = D('MemberProfile')->get_user_details_common($uid);
            $this->myApiPrint(C('STATUS')['200'], 200, $request);
        } else {
            $this->myApiPrint('修改用户资料失败，请稍后再试', 201);
        }
    }

    /**
     * 上传头像
     */
    public function addImage()
    {
        $stu_id['stu_id'] = $this->userId; //获取到用户的ID
        $data = base64imgsave(I('post.img'), $this->userId, 'avatar');
        $this->ajaxReturn($data);
    }

}

